package cn.iocoder.yudao.module.pplan.service.twcleaningplan;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pplan.controller.admin.twcleaningplan.vo.TwCleaningPlanPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.twcleaningplan.vo.TwCleaningPlanRespVO;
import cn.iocoder.yudao.module.pplan.controller.admin.twcleaningplan.vo.TwCleaningPlanSaveReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.twcleaningplan.vo.TwCleaningPlanUpdateStatusReqVO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.tableware.TablewareDO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.twcleaningplan.TwCleaningPlanDO;
import cn.iocoder.yudao.module.pplan.dal.mysql.tableware.TablewareMapper;
import cn.iocoder.yudao.module.pplan.dal.mysql.twcleaningplan.TwCleaningPlanMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.pplan.enums.ErrorCodeConstants.TW_CLEANING_PLAN_NOT_EXISTS;

/**
 * 生产计划-餐具清洗计划 Service 实现类
 *
 * @author yisla
 */
@Service
@Validated
public class TwCleaningPlanServiceImpl implements TwCleaningPlanService {

    @Resource
    private TwCleaningPlanMapper twCleaningPlanMapper;
    @Resource
    private TablewareMapper tablewareMapper;

    @Override
    public Long createTwCleaningPlan(TwCleaningPlanSaveReqVO createReqVO) {
        // 插入
        TwCleaningPlanDO twCleaningPlan = BeanUtils.toBean(createReqVO, TwCleaningPlanDO.class);
        twCleaningPlan.setStartDate(createReqVO.getExpireDate()[0]);
        twCleaningPlan.setEndDate(createReqVO.getExpireDate()[1]);
        twCleaningPlan.setCplanCode("C"+System.currentTimeMillis());
        twCleaningPlanMapper.insert(twCleaningPlan);
        // 返回
        return twCleaningPlan.getId();
    }

    @Override
    public void updateTwCleaningPlan(TwCleaningPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateTwCleaningPlanExists(updateReqVO.getId());
        // 更新
        TwCleaningPlanDO updateObj = BeanUtils.toBean(updateReqVO, TwCleaningPlanDO.class);
        twCleaningPlanMapper.updateById(updateObj);
    }

    @Override
    public void  updateCleaningPlanStatus(@Valid TwCleaningPlanUpdateStatusReqVO updateReqVO) {
        // 校验存在
        validateTwCleaningPlanExists(updateReqVO.getId());
        // 更新
        TwCleaningPlanDO updateObj = BeanUtils.toBean(updateReqVO, TwCleaningPlanDO.class);
        twCleaningPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteTwCleaningPlan(Long id) {
        // 校验存在
        validateTwCleaningPlanExists(id);
        // 删除
        twCleaningPlanMapper.deleteById(id);
    }

    @Override
        public void deleteTwCleaningPlanListByIds(List<Long> ids) {
        // 校验存在
        validateTwCleaningPlanExists(ids);
        // 删除
        twCleaningPlanMapper.deleteByIds(ids);
        }

    private void validateTwCleaningPlanExists(List<Long> ids) {
        List<TwCleaningPlanDO> list = twCleaningPlanMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(TW_CLEANING_PLAN_NOT_EXISTS);
        }
    }

    private void validateTwCleaningPlanExists(Long id) {
        if (twCleaningPlanMapper.selectById(id) == null) {
            throw exception(TW_CLEANING_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public TwCleaningPlanDO getTwCleaningPlan(Long id) {
        return twCleaningPlanMapper.selectById(id);
    }

    @Override
    public PageResult<TwCleaningPlanRespVO> getTwCleaningPlanPage(TwCleaningPlanPageReqVO pageReqVO) {
        if (ObjectUtil.isNotEmpty(pageReqVO.getExpireDate())) {
            pageReqVO.setStartDate(pageReqVO.getExpireDate()[0]);
            pageReqVO.setEndDate(pageReqVO.getExpireDate()[1]);
        }
        PageResult<TwCleaningPlanDO> doPageResult = twCleaningPlanMapper.selectPage(pageReqVO);
        return new PageResult<>(buildTablewareCleaningPlanDetailList(doPageResult.getList()), doPageResult.getTotal());
    }

    private List<TwCleaningPlanRespVO> buildTablewareCleaningPlanDetailList(List<TwCleaningPlanDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        // 1 获取餐具列表
        List<TablewareDO> tablewareDOS = tablewareMapper.selectByIds(convertList(list, TwCleaningPlanDO::getTablewareId));
        Map<Long, TablewareDO> convertMap = CollectionUtils.convertMap(tablewareDOS, TablewareDO::getId);
        // 2. 转换成 VO
        return BeanUtils.toBean(list, TwCleaningPlanRespVO.class, respVO ->
                MapUtils.findAndThen(convertMap, respVO.getTablewareId(), supply -> respVO.setTablewareName(supply.getName())));
    }

}